/**
 * @Title: AnnounceController.java
 * @Package com.zll.ccp.controller
 * @author zhanglinlu
 * @date 2019年5月18日 下午5:22:39
 */
package com.zll.ccp.controller.announce;

import javax.servlet.http.HttpServletRequest;

import com.zll.ccp.constants.Constant;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.zll.ccp.model.Announce;
import com.zll.ccp.service.IAnnounceService;
import com.zll.ccp.utils.CommonUtils;

/**
 * @ClassName: AnnounceController
 * @Description: 公告controller
 * @author zhanglinlu
 * @date 2019年5月18日 下午5:22:39
 * 
 */
@Controller
public class AnnounceController {
    @Autowired
    private IAnnounceService announceService;

    /**
     * @Title: announce
     * @Description: 进入公告编辑页面
     * @param id 公告id
     * @param request
     * @return
     */
    @RequiresRoles(Constant.ROLE_ADMIN)
    @RequestMapping("/announce")
    public String announce(String id, HttpServletRequest request) {
        request.setAttribute("announce", announceService.getAnnounceById(id));
        return "announce/editAnnounce";
    }

    /**
     * @Title: submitannounce
     * @Description: 提交公告
     * @param announce 公告
     * @return
     */
    @RequiresRoles(Constant.ROLE_ADMIN)
    @PostMapping("/submitannounce")
    @ResponseBody
    public Object submitannounce(@RequestBody Announce announce) {
        JSONObject jsonObject = new JSONObject();
        String id = announce.getId();
        if (StringUtils.isBlank(id)) {
            // 新增
            String newId = CommonUtils.getUUID();
            announce.setId(newId);
            announce.setCreateTime(CommonUtils.getNowDate());
            announceService.addAnnounce(announce);
            jsonObject.put("id", newId);
            return jsonObject;
        }
        // 修改
        announceService.updateAnnounce(announce);
        jsonObject.put("id", announce.getId());
        return jsonObject;
    }

    /**
     * @Title: announceInfo
     * @Description: 进入公告详情页
     * @return
     */
    @GetMapping("/announceinfo")
    public String announceInfo(String id, HttpServletRequest request) {
        request.setAttribute("id", id);
        return "announce/announceInfo";
    }

    /**
     * @Title: initannounce
     * @Description: 详情页展示
     * @param id
     * @return
     */
    @GetMapping("/initannounce")
    @ResponseBody
    public Object initannounce(String id) {
        return announceService.getAnnounceById(id);
    }

}
